package com.deng.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.function.RequestPredicates;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;

/**
 * swagger资源文件访问权限设置
 * 在生产环境下将一下请求全部拦截
 * @author
 * @version
 * @date
 */
// 该配置在生产环境下生效
@Profile("prod")
@Configuration
public class RouterFunctionConfiguration {

    @SuppressWarnings("rawtypes")
    @Bean
    public RouterFunction routerFunction() {
        return RouterFunctions.route(
                RequestPredicates.GET("/swagger-resources")
                        .or(RequestPredicates.GET("/swagger-resources/configuration/ui"))
                        .or(RequestPredicates.GET("/swagger-resources/configuration/security"))
                        .or(RequestPredicates.GET("/v2/api-docs"))
                        .or(RequestPredicates.GET("/v3/api-docs"))
                        .or(RequestPredicates.GET("/v3/api-docs/swagger-config"))
                        .and(RequestPredicates.accept(MediaType.ALL)),
                request -> ServerResponse.status(HttpStatus.FORBIDDEN).body(HttpStatus.FORBIDDEN.getReasonPhrase()));
    }
}